package Visual;

import Excepciones.*;
import Logica.Vehiculo;
import javax.swing.JOptionPane;

public class frmAltaVehiculo extends javax.swing.JDialog {
    private Principal unaPrincipal = new Principal();
    ControladoraVisual miControladora = new ControladoraVisual();
    boolean habilitar = true;
    frmAltaMantenimiento mantenimiento;
    public frmAltaVehiculo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);                
        initComponents();
        setTitle("Alta Vehículo");
        this.setLocationRelativeTo(unaPrincipal);
        buttonGroup1.add(this.rbtnAgricola);
        buttonGroup1.add(this.rbtnRodado);
    }

    frmAltaVehiculo(java.awt.Frame parent, boolean modal, boolean habilitar,frmAltaMantenimiento unaVentana) {
        super(parent, modal);                
        initComponents();
        setTitle("Alta Vehículo");
        this.setLocationRelativeTo(unaPrincipal);
        buttonGroup1.add(this.rbtnAgricola);
        buttonGroup1.add(this.rbtnRodado);
        this.habilitar = habilitar;
        this.mantenimiento = unaVentana;//To change body of generated methods, choose Tools | Templates.
    }

    /*frmAltaVehiculo(Principal unaPrincipal, boolean b, boolean b0, frmAltaMantenimiento aThis) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }*/
    
    
       
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblModelo = new javax.swing.JLabel();
        lblMarca = new javax.swing.JLabel();
        lblTara = new javax.swing.JLabel();
        lblCargaMaxima = new javax.swing.JLabel();
        lblNumeroDominio = new javax.swing.JLabel();
        lblAnioCompra = new javax.swing.JLabel();
        txtModelo = new javax.swing.JTextField();
        txtMarca = new javax.swing.JTextField();
        txtTara = new javax.swing.JTextField();
        txtCargaMaxima = new javax.swing.JTextField();
        txtNumeroDominio = new javax.swing.JTextField();
        cmbAnioCompra = new javax.swing.JComboBox();
        rbtnAgricola = new javax.swing.JRadioButton();
        rbtnRodado = new javax.swing.JRadioButton();
        btnAlta = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblHorasUso = new javax.swing.JLabel();
        lblKilometros = new javax.swing.JLabel();
        txtHorasUso = new javax.swing.JTextField();
        txtKilometros = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblModelo.setText("Modelo");

        lblMarca.setText("Marca");

        lblTara.setText("Tara");

        lblCargaMaxima.setText("Carga Máxima");

        lblNumeroDominio.setText("Número Dominio");

        lblAnioCompra.setText("Año de Compra");

        cmbAnioCompra.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2006", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016" }));

        rbtnAgricola.setSelected(true);
        rbtnAgricola.setText("Agrícola");
        rbtnAgricola.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnAgricolaActionPerformed(evt);
            }
        });

        rbtnRodado.setText("Rodado");
        rbtnRodado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnRodadoActionPerformed(evt);
            }
        });

        btnAlta.setText("Alta");
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblHorasUso.setText("Horas de Uso");

        lblKilometros.setText("Kilometros");

        txtKilometros.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btnAlta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addGap(21, 21, 21))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCargaMaxima)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtCargaMaxima))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblAnioCompra)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmbAnioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblModelo)
                                    .addComponent(lblMarca)
                                    .addComponent(lblTara))
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtModelo)
                                    .addComponent(txtMarca)
                                    .addComponent(txtTara)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblNumeroDominio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNumeroDominio))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblHorasUso)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtHorasUso))))
                    .addComponent(rbtnAgricola))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbtnRodado)
                        .addContainerGap(72, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblKilometros)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtKilometros)
                        .addGap(10, 10, 10))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblModelo)
                    .addComponent(txtModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMarca)
                    .addComponent(txtMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTara)
                    .addComponent(txtTara, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCargaMaxima)
                    .addComponent(txtCargaMaxima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumeroDominio)
                    .addComponent(txtNumeroDominio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAnioCompra)
                    .addComponent(cmbAnioCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtnAgricola)
                    .addComponent(rbtnRodado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHorasUso)
                    .addComponent(lblKilometros)
                    .addComponent(txtHorasUso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtKilometros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnAlta))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed
        
        Vehiculo unVehiculo = this.conseguirValores();
        try{
            miControladora.añadirVehiculo(unVehiculo);
            JOptionPane.showMessageDialog(this, "La operación fue realizada con éxito", ":)", JOptionPane.INFORMATION_MESSAGE);
            this.inicializarTextFields();
            if(!habilitar){
                mantenimiento.agregarUnPersonal(unVehiculo);
            }
        }
        catch(YaExiste a){
            JOptionPane.showMessageDialog(this, a.getMessage(), "Lo sentimos, algo salió mal", JOptionPane.WARNING_MESSAGE);
        }
        
    }//GEN-LAST:event_btnAltaActionPerformed

    private void rbtnAgricolaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnAgricolaActionPerformed
        this.txtHorasUso.setEnabled(true);
        this.txtKilometros.setEnabled(false);
    }//GEN-LAST:event_rbtnAgricolaActionPerformed

    private void rbtnRodadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnRodadoActionPerformed
        this.txtHorasUso.setEnabled(false);
        this.txtKilometros.setEnabled(true);
    }//GEN-LAST:event_rbtnRodadoActionPerformed
    
    private void inicializarTextFields(){
        this.txtCargaMaxima.setText("");
        this.txtHorasUso.setText("");
        this.txtKilometros.setText("");
        this.txtMarca.setText("");
        this.txtModelo.setText("");
        this.txtNumeroDominio.setText("");
    }
    
    private Vehiculo conseguirValores(){
        Vehiculo unVehiculo = miControladora.getVehiculo();
        unVehiculo.setModelo(this.txtModelo.getText());
        unVehiculo.setMarca(this.txtMarca.getText());
        unVehiculo.setNroDominio(this.txtNumeroDominio.getText());
        unVehiculo.setTara(Integer.parseInt(txtTara.getText()));
        unVehiculo.setTara(Integer.parseInt(txtTara.getText()));        
        unVehiculo.setCargaMaxima(Integer.parseInt(txtCargaMaxima.getText()));
        String anio = (String) cmbAnioCompra.getSelectedItem();
        unVehiculo.setAñoCompra(Integer.parseInt(anio));
        unVehiculo.setMantenimientos(null);
        return unVehiculo;
    }
    
    public void main() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmAltaVehiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmAltaVehiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmAltaVehiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmAltaVehiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmAltaVehiculo dialog = new frmAltaVehiculo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnCancelar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmbAnioCompra;
    private javax.swing.JLabel lblAnioCompra;
    private javax.swing.JLabel lblCargaMaxima;
    private javax.swing.JLabel lblHorasUso;
    private javax.swing.JLabel lblKilometros;
    private javax.swing.JLabel lblMarca;
    private javax.swing.JLabel lblModelo;
    private javax.swing.JLabel lblNumeroDominio;
    private javax.swing.JLabel lblTara;
    private javax.swing.JRadioButton rbtnAgricola;
    private javax.swing.JRadioButton rbtnRodado;
    private javax.swing.JTextField txtCargaMaxima;
    private javax.swing.JTextField txtHorasUso;
    private javax.swing.JTextField txtKilometros;
    private javax.swing.JTextField txtMarca;
    private javax.swing.JTextField txtModelo;
    private javax.swing.JTextField txtNumeroDominio;
    private javax.swing.JTextField txtTara;
    // End of variables declaration//GEN-END:variables
}
